package com.bcu.calculus.service.serviceImpl;

import com.bcu.calculus.entity.Jurisdiction;
import com.bcu.calculus.entity.Message;
import com.bcu.calculus.mapper.JurisdictionMapper;
import com.bcu.calculus.mapper.SendMapper;
import com.bcu.calculus.service.JurisdictionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class JurisdictionServiceImpl implements JurisdictionService {
    private Logger logger= LoggerFactory.getLogger(getClass());
    @Resource
    private JurisdictionMapper jurisdictionMapper;


    @Override
    public Message<?> insert(Jurisdiction jurisdiction) {
        Jurisdiction j = jurisdictionMapper.selectByPrimaryKey(jurisdiction.getJurisdictionId());
        if (j == null){
            logger.info("权限-----增加权限,jurisdictionId为 "+jurisdiction.getJurisdictionId());
            return jurisdictionMapper.insert(jurisdiction) > 0 ? Message.success("添加权限 "+jurisdiction.getJurisdictionId()+" 成功") : Message.fail("添加权限 "+jurisdiction.getJurisdictionId()+"失败");
        }

        else {
            logger.info("权限-----增加权限时失败，已存在新加jurisdictionId "+jurisdiction.getJurisdictionId());
            return Message.fail("添加失败,请尝试另外的权限编号");
        }
    }


    @Override
    public Message<?> deleteByPrimaryKey(String jurisdictionId) {
        Jurisdiction j = jurisdictionMapper.selectByPrimaryKey(jurisdictionId);
        if (j == null){
            logger.info("权限-----通过关键字删除权限失败,编号不存在,jurisdictionId为 "+jurisdictionId);
            return Message.fail("删除失败,请确认要删除的提交编号是否正确");
        }
        else {
            logger.info("权限-----通过关键字 删除 权限,jurisdictionId为 "+jurisdictionId);
            return jurisdictionMapper.deleteByPrimaryKey(jurisdictionId) > 0 ? Message.success("删除权限 "+jurisdictionId+" 成功") : Message.fail("删除权限 "+jurisdictionId+" 失败");
        }
    }


    @Override
    public Message<?> updateByPrimaryKey(Jurisdiction jurisdiction) {
        Jurisdiction j = jurisdictionMapper.selectByPrimaryKey(jurisdiction.getJurisdictionId());
        if (j == null){
            return Message.fail("修改权限失败,请检查jurisdictionId是否存在");
        }
        else {
            logger.info("权限-----通过关键字 修改 权限,jurisdictionId为 "+jurisdiction.getJurisdictionId());
            return jurisdictionMapper.updateByPrimaryKey(jurisdiction) > 0 ? Message.success("修改权限 "+jurisdiction.getJurisdictionId()+" 成功") : Message.fail("修改权限 "+jurisdiction.getJurisdictionId()+" 失败");
        }
    }


    @Override
    public Message<Jurisdiction> selectByPrimaryKey(String jurisdictionId) {
        logger.info("权限-----通过关键字 查询 权限,jurisdictionId为 "+jurisdictionId);
        return Message.success(null).add(jurisdictionMapper.selectByPrimaryKey(jurisdictionId));
    }


    @Override
    public Message<List<Jurisdiction>> selectAllJurisdiction() {
        logger.info("权限-----查询所有 权限");
            return Message.success(null).add(jurisdictionMapper.selectAllJurisdiction());
        }
}
